﻿using System.Collections.ObjectModel;
using System.Text;

namespace Red.Toolkit.EngineLogsViewer.Data
{
    public enum EngineLogGroupControlCode
    {
        CreateGroup,
        EndGroup,

        ParseError
    }

    public class EngineLogGroupEntry : EngineLogAbstractEntry
    {
        #region Data

        public ObservableCollection<EngineLogAbstractEntry> InternalLogEntries { get; private set; }

        #endregion

        #region Constructor

        public EngineLogGroupEntry()
        {
            InternalLogEntries = new ObservableCollection<EngineLogAbstractEntry>();
        }

        #endregion

        #region Public Methods

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendLine( $"#StartGroup {Category}" );

            for ( int i = 0; i < InternalLogEntries.Count; ++i )
            {
                sb.Append( "\t" );
                sb.AppendLine( InternalLogEntries[i].ToString() );
            }

            sb.Append( "#EndGroup" );

            return sb.ToString();
        }

        #endregion

        #region Overrided Methods

        public override void SetSeverityFilter( SeverityLevel severityFilter )
        {
            if ( severityFilter == SeverityLevel.All )
            {
                IsSeverityVisible = true;
                return;
            }

            IsSeverityVisible = false;
            for ( int i = 0; i < InternalLogEntries.Count; ++i )
            {
                InternalLogEntries[i].SetSeverityFilter( severityFilter );
                IsSeverityVisible |= InternalLogEntries[i].IsSeverityVisible;
            }
        }

        public override void SetThreadIdFilter( string threadIdFilter )
        {
            if ( threadIdFilter == "All" )
            {
                IsThreadIdVisible = true;
                return;
            }

            IsThreadIdVisible = false;
            for ( int i = 0; i < InternalLogEntries.Count; ++i )
            {
                InternalLogEntries[i].SetThreadIdFilter( threadIdFilter );
                IsThreadIdVisible |= InternalLogEntries[i].IsThreadIdVisible;
            }
        }

        public override void SetCategoryFilter( string categoryFilter )
        {
            IsCategoryVisible = false;
            for ( int i = 0; i < InternalLogEntries.Count; ++i )
            {
                InternalLogEntries[i].SetCategoryFilter( categoryFilter );
                IsCategoryVisible |= InternalLogEntries[i].IsCategoryVisible;
            }
        }

        public override void SetTextFilter( string textFilter )
        {
            IsTextVisible = false;
            for ( int i = 0; i < InternalLogEntries.Count; ++i )
            {
                InternalLogEntries[i].SetTextFilter( textFilter );
                IsTextVisible |= InternalLogEntries[i].IsTextVisible;
            }
        }

        #endregion
    }
}
